<?php

/**
 * 
 */
class Reports extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('reports_model');
    }

    public function selll($value = '') {

        $data['data'] = $this->reports_model->selectUSERSS();
        echo "<pre>";
        print_r($data['data']);
        echo "</pre>";
    }

    public function temporaryUpdate() {
        $data['data'] = $this->reports_model->selectLast50Lat();
        ini_set('max_execution_time', 1500);
        echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />";
        foreach ($data['data'] as $key => $value) {
            $latt = substr($value['latitude'], 0, 8);
            $lngg = substr($value['longtitude'], 0, 8);
            $lol = $this->GetAddress($latt, $lngg);
            $id = $value['report_id'];
            $user_id = $value['user_id'];
            $type = $value['type'];
            if ($value['type'] == 5)
                $type = 4;
            else
                $type = $value['type'];
            $datetime_added = $value['datetime_added'];
            $datetime_view = $value['datetime_view'];
            $lat = $value['latitude'];
            $lng = $value['longtitude'];
            $ulica = $this->getStreet($lol['addr']);
            $mesto = $this->getCity($lol['addr']);
            $stat = $this->getState($lol['addr']);
            $ulica_ = $this->stripAccents($this->getStreet($lol['addr']));
            $mesto_ = $this->stripAccents($this->getCity($lol['addr']));
            $area_id = $this->AreaToId($lol['region']);
            $zdroj = $value['source'];
            $description = $value['desc'];
            echo "INSERT INTO reports VALUES(
					'',
					'" . $user_id . "',
					'" . $type . "',
					'" . $area_id . "',
					'" . $lat . "',
					'" . $lng . "',
					'" . $ulica . "',
					'" . $mesto . "',
					'" . $stat . "',
					'" . $ulica_ . "',
					'" . $mesto_ . "',
					'" . $datetime_view . "',
					'" . $datetime_added . "',
					'" . $description . "',
					'" . $zdroj . "',
					'0',
					'0',
					'0',
					'0');<br />";
            sleep(1);
        }

        // $this->load->view('temporary/convert',$data);
    }

    private function GetAddress($lat, $lng) {
        $URL = "http://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&sensor=false";
        $data = file($URL);
        $pocitadlo = 0;
        foreach ($data as $line_num => $line) {
            if (strstr($line, 'Region') && $pocitadlo == 0) {
                $region = substr($line, 30);
                $region = substr_replace($region, '', -10);
                $pocitadlo++;
            }

            if (false != strstr($line, 'formatted_address')) {
                $addr = substr(trim($line), 23, -2);
                if (empty($region)) {
                    $pocitadlo = 0;
                    foreach ($data as $line_num => $line) {
                        if (strstr($line, 'Region') && $pocitadlo == 0) {
                            $region = substr($line, 30);
                            $region = substr_replace($region, '', -10);
                            $pocitadlo++;
                        }
                    }
                }
                break;
            }
        }
        if (empty($region))
            $region = 'unknown';
        return array('addr' => $addr, 'region' => $region);
    }

    private function AreaToId($area) {
        $area = trim($area);
        switch ($area) {
            case 'Prešov':
                $area_id = 4;
                break;
            case 'Žilina':
                $area_id = 8;
                break;
            case 'Nitra':
                $area_id = 9;
                break;
            case 'Trenčín':
                $area_id = 5;
                break;
            case 'Košice':
                $area_id = 3;
                break;
            case 'Bratislava':
                $area_id = 7;
                break;
            case 'Banská Bystrica':
                $area_id = 2;
                break;
            case 'Trnava':
                $area_id = 6;
                break;
            default:
                $area_id = 1;
                break;
        }
        return $area_id;
    }

    public function getStreet($input) {
        $part = explode(",", $input);
        if (count($part) == 4) {
            return $part[1];
        } else {
            return $part[0];
        }
    }

    public function getCity($input) {
        $part = explode(",", $input);
        if (count($part) == 3) {
            return ltrim($part[1]);
        } else if (count($part) == 4) {
            return ltrim($part[2]);
        } else {
            return null;
        }
    }

    public function getState($input) {
        $part = explode(",", $input);
        if (count($part) == 3) {
            return ltrim($part[2]);
        } else if (count($part) == 4) {
            return ltrim($part[3]);
        } else {
            return ltrim($part[1]);
        }
    }

    function stripAccents($str) {
        $a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê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ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ');
        $b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
        return str_replace($a, $b, $str);
    }

    public function checkNewest() {
        $id = $this->input->post('id');
        $dat = $this->reports_model->fetchReportsNewerThanId($id);
        echo $dat['count'];
    }

    // report report...
    public function report($report_id) {
        $user_id = $this->session->userdata("sess_user_id");
        $this->reports_model->reportReport($user_id, $report_id);
    }

}

?>